package secondtest;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class Common {

    public static SqlSessionFactory getSessionFactory() {
        SqlSessionFactory factory = null;
        try {
            //1.加载全局配置
            String SqlMapConfigPath = "secondtest/SqlMapConfig.xml";
            //2.得到配置文件的输入流
            InputStream inputStream = Resources.getResourceAsStream(SqlMapConfigPath);

            //3.通过这个流和Builder构建一个新的SqlSessionFactory。这个工厂可以单例
            factory = new SqlSessionFactoryBuilder().build(inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return factory;
    }

}
